var app = getApp()
Page({

  data: {
    curImgUrl: '',
    imgs: []
  },
  onLoad: function(options) {
    var objectStr = decodeURIComponent(options.object)
    var object = JSON.parse(objectStr)
    console.log("height==" + app.globalData.height)
    this.setData({
      imgs: object.imgs,
      curImgUrl: object.imgs[0],
      height: app.globalData.height
    })
    console.log("objectStr====" + objectStr)
  },

  onShow: function() {

  },
  bindPre: function() {
    wx.previewImage({
      current: this.data.curImgUrl,
      urls: this.data.imgs
    })
  },
  bindDownload: function() {
    wx.showLoading({
      title: '正在下载中',
    })
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          console.log('没有获取授权');
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: (res) => {
              this.save(this.data.curImgUrl)
            }
          })
        } else {
          console.log('已获取授权');
          this.save(this.data.curImgUrl)
        }
      }
    });
  },

  save: function(url) {
    let pos = url.lastIndexOf('\/'); // 查找最后一个/的位置
    var name = url.substring(pos + 1); 
    console.log("name====="+name)
    const downloadTask=wx.downloadFile({
      url: url,
      success(res) {
        console.log(res)
        var savePath = wx.env.USER_DATA_PATH + "/"+name
        wx.getFileSystemManager()
          .saveFile({ //下载成功后保存到本地
            tempFilePath: res.tempFilePath,
            filePath: savePath,
            success(res2) {
              wx.hideLoading()
              wx.saveImageToPhotosAlbum({
                filePath: savePath,
                success: (res) => {
                  wx.showModal({
                    title: '文件已保存到手机相册',
                    content: '位于tencent/MicroMsg/WeiXin下',
                    confirmColor: '#0bc183',
                    confirmText: '知道了',
                    showCancel: false
                  })
                },
                fail(res) {
                  console.log(res)
                }
              })
            },
            fail(res) {
              wx.hideLoading()
              console.log(res)
            }
          })
      },
      fail(res) {
        wx.hideLoading()
        console.log(res)
      }
    })
  },


  cardSwiper: function(e) {
    console.log("eeeeee==" + JSON.stringify(e))
    this.setData({
      curImgUrl: this.data.imgs[e.detail.current]
    })
  },
  bindOpenPreImg: function() {
    wx.previewImage({
      current: this.data.curImgUrl,
      urls: this.data.imgs
    })
  },
  onShareAppMessage: function() {
    return {
      title: '美图欣赏，欢迎加入',
      imageUrl: this.data.curImgUrl,
      path: '/pages/index/index'
    }
  }
})